//页面加载完毕
$(function () {

    var officialStudent_datagrid = $("#officialStudent_datagrid");
    var officialStudent_dialog = $("#officialStudent_dialog");
    var officialStudent_form = $("#officialStudent_form");
    var changeClass_dialog = $("#changeClass_dialog");
    var changeClass_form = $("#changeClass_form");
    var studentLoss_dialog = $("#studentLoss_dialog");
    var studentLoss_form = $("#studentLoss_form");

    //正式学员列表初始化
    officialStudent_datagrid.datagrid({
        url:"/officialStudent/list.do",
        fit:true,
        fitColumns:true,
        striped:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        toolbar:"#tb",
        columns:[[
            {field:"id", hidden:true},
            {field:"name", title:"学员名称", width:80,align:'center'},
            {field:"marketingPerson", title:"销售人员", width:80,align:'center',formatter:marketingEmpFormatter},
            {field:"entranceTime", title:"入学时间", width:80,align:'center'},
            {field:"totalTuition", title:"总学费", width:80,align:'center',formatter:totalTuitionA},
            {field:"hasDealTuition", title:"已交学费", width:80,align:'center'},
            {field:"noDealTuition", title:"未缴学费", width:80,align:'center'},
            {field:"dealTuitionState", title:"缴费状态", width:80,formatter:dealTuitionStateA,align:'center'},
            {field:"schoolName", title:"学校", width:80,align:'center'},
            {field:"tel", title:"电话", width:80,align:'center'},
            {field:"email", title:"邮箱", width:80,align:'center'},
            {field:"clazz", title:"班级", width:80,align:'center,',formatter:clazzA},
            {field:"paymentType", title:"支付方式", width:80,formatter:paymentTypeA,align:'center'},
            {field:"studyState", title:"状态", width:80,formatter:studyStateA,align:'center'}
        ]],
        onClickRow: function (index, row) {
            var id = row.studyState.id;
            if(id == 66){
                //在读
                $('#jjj').hide();
                $('#hhh').show();
                $('#iii').show();
                $('#kkk').show();

            }else if(id == 67){
                //转班中
                $('#hhh').hide();
                $('#iii').hide();
                $('#jjj').hide();
                $('#kkk').hide();
            }else if(id == 68){
                //休学
                $('#jjj').show();
                $('#kkk').show();
                $('#hhh').hide();
                $('#iii').hide();
            }else if(id == 69){
                //流失
                $('#hhh').hide();
                $('#iii').hide();
                $('#jjj').hide();
                $('#kkk').hide();
            }else{

            }
        }
    });
    //end

    var cmdObj = {
        //正式学员查看
        lookView : function (){
            //判断是否选中
            var row = officialStudent_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条查看数据", "info");
                return;
            }
            console.log(row);

            //添加对话框打开
            officialStudent_dialog.dialog("open");
            //修改对话框标题
            officialStudent_dialog.dialog("setTitle","正式学员信息");

            //营销人员
            if(row.marketingPerson){
                row["marketingPerson.id"] = row.marketingPerson.id;
            }
            //性别
            if(row.gender){
                row["gender.id"] = row.gender.id;
            }
            //学校客户
            if(row.schoolClient){
                row["schoolClient.id"] = row.schoolClient.id;
            }
            //学历
            if(row.education){
                row["education.id"] = row.education.id;
            }
            //客户来源
            if(row.clientSource){
                row["clientSource.id"] = row.clientSource.id;
            }
            //客户类型
            if(row.clientType){
                row["clientType.id"] = row.clientType.id;
            }
            //班级
            if(row.clazz){
                row["clazz.id"] = row.clazz.id;
            }
            //意向校区
            if(row.campus){
                row["campus.id"] = row.campus.id;
            }
            //就业意向
            if(row.jobIntention){
                row["jobIntention.id"] = row.jobIntention.id;
            }
            //学习状态
            if(row.studyState){
                row["studyState.id"] = row.studyState.id;
            }
            //支付方式
            if(row.paymentType){
                row["paymentType.id"] = row.paymentType.id;
            }

            //数据回显
            officialStudent_form.form("load",row);
            //隐藏保存按钮
            $('.dialog-button a:eq(0)').hide();
            //设置只读
            $(".readOn").textbox('readonly',true);
        },
        //编辑操作
        edit : function () {
            //判断是否选中
            var row = officialStudent_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条编辑数据", "info");
                return;
            }

            //添加对话框打开
            officialStudent_dialog.dialog("open");
            //修改对话框标题
            officialStudent_dialog.dialog("setTitle","编辑正式学员");

            //营销人员
            if(row.marketingPerson){
                row["marketingPerson.id"] = row.marketingPerson.id;
            }
            //性别
            if(row.gender){
                row["gender.id"] = row.gender.id;
            }
            //学校客户
            if(row.schoolClient){
                row["schoolClient.id"] = row.schoolClient.id;
            }
            //学历
            if(row.education){
                row["education.id"] = row.education.id;
            }
            //客户来源
            if(row.clientSource){
                row["clientSource.id"] = row.clientSource.id;
            }
            //客户类型
            if(row.clientType){
                row["clientType.id"] = row.clientType.id;
            }
            //班级
            if(row.clazz){
                row["clazz.id"] = row.clazz.id;
            }
            //意向校区
            if(row.campus){
                row["campus.id"] = row.campus.id;
            }
            //就业意向
            if(row.jobIntention){
                row["jobIntention.id"] = row.jobIntention.id;
            }
            //学习状态
            if(row.studyState){
                row["studyState.id"] = row.studyState.id;
            }
            //支付方式
            if(row.paymentType){
                row["paymentType.id"] = row.paymentType.id;
            }

            //数据回显
            officialStudent_form.form("load",row);
            //取消只读
            $(".readOn").textbox('readonly',false);
            //取消隐藏按钮
            $('.dialog-button a:eq(0)').show();

        },
        //正式学员删除
        removeT : function () {
            //判断是否选中
            var row = officialStudent_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条删除数据", "info");
                return;
            }
            $.messager.confirm('温馨提示','您确认想要删除记录吗？',function(ret){
                if (ret){
                    //确认删除
                    $.get("/officialStudent/delete.do?id=" + row.id,function (data) {
                        if(data.success){
                            //删除成功
                            $.messager.confirm('温馨提示','删除成功!',function(ret){
                                //刷新数据
                                officialStudent_datagrid.datagrid("reload");
                            });

                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //正式学员添加/更新
        save : function (){
            //表单提交url
            var url = "";
            //使用id区分保存于更新操作
            var id = $("#aaaaId").val();
            console.log(id);
            if(id){
                //编辑
                url = "/officialStudent/update.do";
            }else{
                $.messager.alert('温馨提示',"操作失败!", "info");
                officialStudent_dialog.dialog("close");
                officialStudent_datagrid.datagrid("reload");
                return;
            }
            //提交表单
            officialStudent_form.form("submit", {
                url:url,
                success:function (data) {
                    //jquery将字符串转换json对象
                    //data必须是标准的json字符串
                    data = $.parseJSON(data);
                    if(data.success){
                        //提示成功
                        $.messager.confirm('温馨提示',"操作成功",function(r){
                            //关闭窗口
                            officialStudent_dialog.dialog("close");
                            //刷新数据
                            officialStudent_datagrid.datagrid("reload");
                        });
                    }else{
                        $.messager.alert('温馨提示',data.msg, "error");
                    }
                }

            })
        },
        //高级查询  思路: 加载正式学员列表时  将多条件查询数据一同发起请求
        searchT : function () {
            var keyword = $("#keyword").val();
            var clazzId = $("#clazzId").val();
            var beginDate = $("#beginDate").val();
            var endDate = $("#endDate").val();
            //加载数据列表时 携带额外参数
            officialStudent_datagrid.datagrid("load",{
                keyword:keyword ,
                clazzId:clazzId ,
                beginDate:beginDate ,
                endDate:endDate
            })
        },
        //取消操作
        cancel : function (){
            officialStudent_dialog.dialog("close");
        },
        //班级转换对话框关闭
        cancelChange : function (){
            changeClass_dialog.dialog("close");
            //清除缓存数据
            changeClass_form.form("clear");
        },
        //流失学员对话框关闭
        cancelLossStu : function (){
            studentLoss_dialog.dialog("close");
            //清除缓存数据
            studentLoss_form.form("clear");
        },
        //刷新
        reload : function (){
            officialStudent_datagrid.datagrid("reload");
        },
        //转换班级
        changeClassA : function () {
            //判断是否选中
            var row = officialStudent_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条数据", "info");
                return;
            }
            //转班对话框打开
            changeClass_dialog.dialog("open");
            //清除缓存数据
            changeClass_form.form("clear");
            //修改对话框标题
            changeClass_dialog.dialog("setTitle","学员转班信息填写");
            //班级
            if(row.clazz){
                row["clazz.id"] = row.clazz.id;
            }
            //数据回显
            changeClass_form.form("load",row);
        },
        //班级转换提交
        saveChange :function () {
            //提交表单
            changeClass_form.form("submit", {
                url:'/officialStudent/saveChangeClass.do',
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        //提示成功
                        $.messager.confirm('温馨提示',"操作成功",function(r){
                            //关闭窗口
                            changeClass_dialog.dialog("close");
                            //刷新数据
                            officialStudent_datagrid.datagrid("reload");
                        });
                    }else{
                        $.messager.alert('温馨提示',data.msg, "error");
                        //关闭窗口
                        changeClass_dialog.dialog("close");
                        //刷新数据
                        officialStudent_datagrid.datagrid("reload");
                    }
                }

            })
        },
        //休学
        quitClass :function () {
            //判断是否选中
            var row = officialStudent_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条数据", "info");
                return;
            }
            $.messager.confirm('温馨提示','是否确认休学？',function(ret) {
                $.post('/officialStudent/quitClass.do?id=' + row.id, function (data) {
                    if (data.success) {
                        //成功
                        $.messager.confirm('温馨提示', '休学成功!', function (ret) {
                            //刷新数据
                            officialStudent_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "error");
                    }
                })
            })
        },
        //复学
        recoverClass : function () {
            //判断是否选中
            var row = officialStudent_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条数据", "info");
                return;
            }
            $.messager.confirm('温馨提示','是否确认复学？',function(ret) {
                $.post('/officialStudent/recoverClass.do?id=' + row.id, function (data) {
                    if (data.success) {
                        //成功
                        $.messager.confirm('温馨提示', '复学成功!', function (ret) {
                            //刷新数据
                            officialStudent_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "error");
                    }
                })
            })
        },
        //学员流失
        drain : function () {
            //判断是否选中
            var row = officialStudent_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择一条数据", "info");
                return;
            }
            //转班对话框打开
            studentLoss_dialog.dialog("open");
            //清除缓存数据
            studentLoss_form.form("clear");
            //修改对话框标题
            studentLoss_dialog.dialog("setTitle","学员流失信息填写");
            //班级
            if(row.clazz){
                row["clazz.id"] = row.clazz.id;
            }
            //数据回显
            studentLoss_form.form("load",row);
        },
        //学员流失提交
        saveLossStu : function () {
            //提交表单
            studentLoss_form.form("submit", {
                url:'/officialStudent/saveLossStu.do',
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        //提示成功
                        $.messager.confirm('温馨提示',"操作成功",function(r){
                            //关闭窗口
                            studentLoss_dialog.dialog("close");
                            //刷新数据
                            officialStudent_datagrid.datagrid("reload");
                        });
                    }else{
                        $.messager.alert('温馨提示',data.msg, "error");
                        //关闭窗口
                        studentLoss_dialog.dialog("close");
                        //刷新数据
                        officialStudent_datagrid.datagrid("reload");
                    }
                }

            })
        }
    };

    //调用
    //给所有的按钮统一绑定事件
    $("a[data-cmd]").on("click",function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

    //对话框初始化
    officialStudent_dialog.dialog({
        width: "60%",
        height: "70%",
        buttons:"#bb",
        closable:true,
        closed:true
    });
    //班级转换对话框初始化
    changeClass_dialog.dialog({
        width: "330px",
        height: "300px",
        buttons:"#cc",
        closable:true,
        closed:true
    });
    //流失学员对话框初始化
    studentLoss_dialog.dialog({
        width: "330px",
        height: "440px",
        buttons:"#dd",
        closable:true,
        closed:true
    });
    //end

});

//缴费状态格式化
function dealTuitionStateA(value, row, index){
    if(row.noDealTuition == 0){
        return "<span style='color: #00ee00;'>已缴清</span>"
    }
    return "<span style='color: red'>未缴清</span>";
}
//学习状态格式化
function studyStateA(value, row, index){
    //66在读 67转班 68休学 69流失
    if(value.id == 66){
        return "<span style='color: #00ee00'>"+value.name+"</span>";
    }else if(value.id == 67){
        return "<span style='color: #0000FF'>"+value.name+"</span>";
    }else if(value.id == 68){
        return "<span style='color:orangered'>"+value.name+"</span>";
    }else if(value.id == 69){
        return "<span style='color: red'>"+value.name+"</span>";
    }else{
    }
}
//销售人员格式化
function marketingEmpFormatter(value, row, index){
    return value ? value.username : "";
}
//总学费格式化
function totalTuitionA(value, row, index){
    return value ? value.name : "";
}
//班级格式化
function clazzA(value, row, index){
    return value ? value.name : "";
}

//支付方式格式化
function paymentTypeA(value, row, index){
    return value ? value.name : "";
}
//快查
function quickCheck(a) {
    $("#officialStudent_datagrid").datagrid("load",{
        studyStateId: a
    });
}
